package com.seckill.goods.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seckill.goods.pojo.Category;
import com.seckill.goods.service.CategoryService;
import com.seckill.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品类目 控制器</p>
 *
 * @author 作者
 * @since 2022-03-04
 */
@RestController
@RequestMapping("/category")
public class CategoryController {


    @Autowired
    private CategoryService categoryService;

    /***
     * Category分页条件搜索实现
     * @param category
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<Page<Category>> findPage(@RequestBody(required = false) Category category, @PathVariable  int page, @PathVariable  int size){
        //调用CategoryService实现分页条件查询Category
        Page<Category> pageInfo = categoryService.findPage(category, page, size);
        return Result.ok(pageInfo);
    }

    /***
     * Category分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<Page<Category>> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用CategoryService实现分页查询Category
        Page<Category> pageInfo = categoryService.findPage(page, size);
        return Result.ok(pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param category
     * @return
     */
    @PostMapping(value = "/search" )
    public Result<List<Category>> findList(@RequestBody(required = false)  Category category){
        //调用CategoryService实现条件查询Category
        List<Category> list = categoryService.findList(category);
        return Result.ok(list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        //调用CategoryService实现根据主键删除
        categoryService.delete(id);
        return Result.ok();
    }

    /***
     * 修改Category数据
     * @param category
     * @param id
     * @return
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody  Category category,@PathVariable Integer id){
        //设置主键值
        category.setId(id);
        //调用CategoryService实现修改Category
        categoryService.update(category);
        return Result.ok();
    }

    /***
     * 新增Category数据
     * @param category
     * @return
     */
    @PostMapping
    public Result add(@RequestBody   Category category){
        //调用CategoryService实现添加Category
        categoryService.add(category);
        return Result.ok();
    }

    /***
     * 根据ID查询Category数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Category> findById(@PathVariable Integer id){
        //调用CategoryService实现根据主键查询Category
        Category category = categoryService.findById(id);
        return Result.ok(category);
    }

    /***
     * 根据ID查询Category数据
     * @param id
     * @return
     */
    @GetMapping("/parent/{id}")
    public Result<List<Category>> findByParentId(@PathVariable Integer id){
        //调用CategoryService实现根据父类ID查询Category
        List<Category> categorys = categoryService.findByParentId(id);
        return Result.ok(categorys);
    }

    /***
     * 查询Category全部数据
     * @return
     */
    @GetMapping
    public Result<List<Category>> findAll(){
        //调用CategoryService实现查询所有Category
        List<Category> list = categoryService.findAll();
        return Result.ok(list);
    }
}

